package com.baizhi.service.impl;

import com.baizhi.entity.Category;
import com.baizhi.mapper.CategoryMapper;
import com.baizhi.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author 2107class
 * @since 2021-12-22
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> query() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.isNull("parent_id");

        List<Category> categories = categoryMapper.selectList(wrapper);

        for (Category category : categories) {
            QueryWrapper<Category> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",category.getId());
            List<Category> categories1 = categoryMapper.selectList(wrapper1);

            category.setChildren(categories1);
        }

        return categories;
    }

    @Override
    public Category queryOne(Integer id) {
        Category category = categoryMapper.selectById(id);
        return category;
    }
}
